package top.huxy.boot.util;

import nl.basjes.parse.useragent.UserAgent;
import nl.basjes.parse.useragent.UserAgentAnalyzer;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @Classname RequestUtils
 * @Description 这个类可以获取到一些请求信息
 * @Date 2021/9/24 8:11
 * @Created by huxy
 */
public class RequestUtils {
    private static UserAgentAnalyzer uaa;

    static {
        uaa = UserAgentAnalyzer
                .newBuilder()
                .hideMatcherLoadStats()
                .withField("OperatingSystemNameVersionMajor")
                .withField("AgentNameVersion")
                .build();
    }

    /**
     * 获取当前线程处理的请求。可能为空
     *
     * @return
     */
    public static HttpServletRequest getCurrentRequest() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) return null;
        return requestAttributes.getRequest();
    }

    /**
     * 获取或生成请求id
     *
     * @return 一个随机的id
     */
    public static String getRequestID(HttpServletRequest request) {
        if (request == null) return null;
        Object requestID = request.getAttribute("requestID");
        if (StringUtils.isEmpty(requestID)) request.setAttribute("requestID", StringUtils.unique());
        return request.getAttribute("requestID").toString();
    }

    /**
     * 获取或生成当前线程的请求的id
     */
    public static String getRequestID() {
        return getRequestID(getCurrentRequest());
    }


    /**
     * 从User-Agent解析客户端操作系统和浏览器版本
     *
     * @param userAgent
     * @return
     */
    public static Map<String, String> parseOsAndBrowser(String userAgent) {
        UserAgent agent = uaa.parse(userAgent);
        String os = agent.getValue("OperatingSystemNameVersionMajor");
        String browser = agent.getValue("AgentNameVersion");
        Map<String, String> map = new HashMap<>(2);
        map.put("os", os);
        map.put("browser", browser);
        return map;
    }

    public static Map<String, String> parseOsAndBrowser(HttpServletRequest request) {
        return parseOsAndBrowser(getUserAgent(request));
    }

    public static Map<String, String> parseOsAndBrowser() {
        HttpServletRequest currentRequest = getCurrentRequest();
        if (currentRequest == null) return null;
        String userAgent = getUserAgent(currentRequest);
        return parseOsAndBrowser(userAgent);
    }

    /**
     * 从请求头中获取User-Agent
     */
    public static String getUserAgent(HttpServletRequest request) {
        if (request == null) return null;
        return request.getHeader("User-Agent");
    }

    public static String getUserAgent() {
        return getUserAgent(getCurrentRequest());
    }

    public static boolean hasRequestId(HttpServletRequest request) {
        return request.getAttribute("requestID") != null;
    }

    public static HttpServletResponse getCurrentResponse() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {
            return null;
        }
        return requestAttributes.getResponse();
    }
}
